package com.example.excel.ui.fragment.gallery;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

import com.example.excel.R;
import com.example.excel.base.BaseFragment;
import com.example.excel.bean.InventoryBean;
import com.example.excel.config.Constants;
import com.example.excel.databinding.FragmentGalleryBinding;
import com.example.excel.ui.adapter.FormAdapter2;
import com.example.excel.ui.adapter.FormHelper;
import com.example.excel.ui.adapter.GalleryTitleAdapter;
import com.example.excel.utils.Utils;
import com.example.excel.view.FormLayoutManger2;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.Arrays;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

public class GalleryFragment extends BaseFragment<FragmentGalleryBinding, GalleryViewModel> {

    private FormAdapter2 adapter;
    private int index;
    int allPageCount;

    public static GalleryFragment getInstance(int index) {
        GalleryFragment fragment = new GalleryFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("INDEX", index);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected Class<GalleryViewModel> initViewModel() {
        return GalleryViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_gallery;
    }

    @Override
    protected void initView() {
        index = getArguments().getInt("INDEX", 0);
        mBinding.recyclerTitleView.setLayoutManager(new FormLayoutManger2(getContext()));
        GalleryTitleAdapter adapterTitle = new GalleryTitleAdapter();
        mBinding.recyclerTitleView.setAdapter(adapterTitle);
        FormLayoutManger2 formLayoutManger2 = new FormLayoutManger2(getContext(), mBinding.recyclerView, mBinding.recyclerTitleView);
        formLayoutManger2.setRefreshLayout(mBinding.swipeLayout);
        mBinding.recyclerView.setLayoutManager(formLayoutManger2);
        adapter = new FormAdapter2();
        mBinding.recyclerView.setAdapter(adapter);

        mBinding.recyclerView.setNestedScrollingEnabled(false);
        mBinding.swipeLayout.setEnableAutoLoadMore(false);
        mBinding.swipeLayout.setEnableLoadMore(false);
        // 下拉刷新
        mBinding.swipeLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mViewModel.listByDB(index, mViewModel.mPageNum);
            }
        });
    }

    @Override
    protected void lazyInit() {
        mViewModel.listByDB(index, 0);
    }

    @Override
    protected void registerObserve() {
        super.registerObserve();
        mViewModel.mBeans.observe(this, new Observer<List<InventoryBean>>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onChanged(List<InventoryBean> data) {
                adapter.mData.clear();
                adapter.notifyDataSetChanged();
                if (data != null && data.size() > 0) {
                    adapter.setData(data);
                    if (mViewModel.mTotal > -1) {
                        allPageCount = getAllPageCount(mViewModel.mTotal, Constants.PAGE_SIZE);
                        mBinding.tvPage.setText(String.format("%s / %s", mViewModel.mPageNum + 1, allPageCount));
                    } else {
                        mBinding.tvPage.setText(String.format("%s / %s", 0, 0));
                    }
                } else {
                    if (index == 0) {
                        new AlertDialog.Builder(getActivity()).setMessage("暂无已盘点数据").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).create().show();
                    }
                }
            }
        });
        mViewModel.mStopRefresh.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    mBinding.swipeLayout.finishRefresh(true);
                }
            }
        });
        mBinding.tvNext0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onPrePage(v);
            }
        });
        mBinding.tvNext1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onNextPage(v);
            }
        });
    }

    /**
     * 获取总页数
     */
    private int getAllPageCount(int allCount, int pageSize) {
        return (allCount - 1) / pageSize + 1;
    }

    public void onPrePage(View view) {
        if (mViewModel.mPageNum > 0) {
            int page = mViewModel.mPageNum - 1;
            mViewModel.listByDB(index, page);
        } else {
            Utils.getInstance().showToast("当前为最小页码");
        }
    }

    public void onNextPage(View view) {
        if (mViewModel.mPageNum < allPageCount - 1) {
            int page = mViewModel.mPageNum + 1;
            mViewModel.listByDB(index, page);
        } else {
            Utils.getInstance().showToast("当前为最大页码");
        }
    }
}
